package com.originskyseed.platform.count.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.count.mapper.CountAnchorAndSalesRankListMapper;
import com.originskyseed.platform.count.service.ICountAnchorAndSalesRankListService;
import com.originskyseed.platform.count.vo.CountAnchorAndSalesRankListVO;
import com.originskyseed.platform.user.mapper.DeptMapper;
import com.originskyseed.platform.user.model.Dept;

/**
 * Service接口实现类（COUNTANCHORANDSALESRANKLIST）
 *
 * @author wlj
 */
@DubboService
@Component("countAnchorAndSalesRankListService")
public class CountAnchorAndSalesRankListServiceImpl implements ICountAnchorAndSalesRankListService {

	@Resource
	private CountAnchorAndSalesRankListMapper countAnchorAndSalesRankListMapper;

	@Resource
	private DeptMapper deptMapper;

	/**
	 * 优质用户榜排行列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CountAnchorAndSalesRankListVO> selectHighPlayerRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		params.put("page", page);
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectHighPlayerRankListByPage(params);
		this.topDeptName(list);
		page.setResults(list);
		return page;
	}

	/**
	 * 推广平台优质用户榜排行列表导出
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CountAnchorAndSalesRankListVO> selectHighPlayerRankList(Map<String, Object> params) throws Exception {
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectHighPlayerRankListByPage(params);
		this.topDeptName(list);
		return list;
	}

	/**
	 * 有效注册榜排行列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CountAnchorAndSalesRankListVO> selectValidPlayerRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		params.put("page", page);
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectValidPlayerRankListByPage(params);
		this.topDeptName(list);
		page.setResults(list);
		return page;
	}

	/**
	 * 推广平台有效注册榜排行列表导出
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CountAnchorAndSalesRankListVO> selectValidPlayerRankList(Map<String, Object> params) throws Exception {
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectValidPlayerRankListByPage(params);
		this.topDeptName(list);
		return list;
	}

	/**
	 * 推广平台三天登录榜排行列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CountAnchorAndSalesRankListVO> selectThirdDayLoginPlayerRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		params.put("page", page);
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectThirdDayLoginPlayerRankListByPage(params);
		this.topDeptName(list);
		page.setResults(list);
		return page;
	}

	/**
	 * 推广平台三天登录榜排行列表导出
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CountAnchorAndSalesRankListVO> selectThirdDayLoginPlayerRankList(Map<String, Object> params) throws Exception {
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectThirdDayLoginPlayerRankListByPage(params);
		this.topDeptName(list);
		return list;
	}

	/**
	 * 推广平台七天登录榜排行列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CountAnchorAndSalesRankListVO> selectSeventhDayLoginPlayerRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		params.put("page", page);
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectSeventhDayLoginPlayerRankListByPage(params);
		this.topDeptName(list);
		page.setResults(list);
		return page;
	}

	/**
	 * 推广平台七天登录榜排行列表导出
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CountAnchorAndSalesRankListVO> selectSeventhDayLoginPlayerRankList(Map<String, Object> params) throws Exception {
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectSeventhDayLoginPlayerRankListByPage(params);
		this.topDeptName(list);
		return list;
	}

	/**
	 * 主播直播排行榜（个人、排麦）
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public BasePage<CountAnchorAndSalesRankListVO> selectAnchorLiveRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		List<CountAnchorAndSalesRankListVO> rankList = new ArrayList<>();
		params.put("page", page);
		List<String> deptIdList = getDeptIdList(params);
		params.put("deptIdList", deptIdList);
		String deptCode = "";
		if (params.containsKey("deptCode")) {
			deptCode = params.get("deptCode") == null ? "" : params.get("deptCode").toString();
		}
		if (StringUtils.isNotEmpty(deptCode) && (deptIdList == null || deptIdList.size() == 0)) {
			return page;
		} else {
			rankList = this.countAnchorAndSalesRankListMapper.selectAnchorLiveRankListByPage(params);
		}
		if (rankList != null && rankList.size() > 0) {
			Map map = new HashMap();
			for (int i = 0; i < rankList.size(); i++) {
				CountAnchorAndSalesRankListVO rankListVO = rankList.get(i);

				rankListVO.setDeptName(getDeptName(deptCode, map, rankListVO));

				getCountAnchorAndSalesRankListVOTime(rankListVO);
			}
		}
		page.setResults(rankList);
		return page;
	}

	private String getDeptName(String deptCode, Map map, CountAnchorAndSalesRankListVO rankListVO) throws Exception {
		String name = "";
		String deptId = rankListVO.getDeptId();
		if (map.containsKey(deptId)) {
			name = map.get(deptId).toString();
		} else {
			Dept dept = deptMapper.selectByPrimaryKey(deptId);
			if (dept != null) {
				Dept resultDept = this.selectTopDept(deptCode, dept.getCode());
				name = resultDept == null ? "" : resultDept.getName();
			}
			map.put(deptId, name);
		}
		return name;
	}

	private void getCountAnchorAndSalesRankListVOTime(CountAnchorAndSalesRankListVO rankListVO) {
		int liveSecond = rankListVO.getLiveSecond();
		int hours = liveSecond / (60 * 60);
		int minutes = liveSecond % (60 * 60) / 60;
		int seconds = liveSecond % (60 * 60) % 60;
		if (hours > 0) {
			if (minutes > 0) {
				if (seconds > 0) {
					rankListVO.setLiveTime(hours + "小时" + minutes + "分" + seconds + "秒");
				} else {
					rankListVO.setLiveTime(hours + "小时" + minutes + "分");
				}
			} else {
				if (seconds > 0) {
					rankListVO.setLiveTime(hours + "小时" + seconds + "秒");
				} else {
					rankListVO.setLiveTime(hours + "小时");
				}
			}
		} else {
			if (minutes > 0) {
				if (seconds > 0) {
					rankListVO.setLiveTime(minutes + "分" + seconds + "秒");
				} else {
					rankListVO.setLiveTime(minutes + "分");
				}
			} else {
				rankListVO.setLiveTime(seconds + "秒");
			}
		}
	}

	/**
	 *
	 * @param deptCode
	 * @return
	 * @throws Exception
	 */
	private List<Dept> getAllDept(List<Dept> deptList, String deptCode) throws Exception {
		if (deptCode == null || StringUtils.isEmpty(deptCode)) {
			return deptList;
		} else {
			List<Dept> tempDeptList = deptMapper.selectByParentCode(deptCode);
			List<Dept> tempDeptList0 = new ArrayList<>();
			if (tempDeptList != null && tempDeptList.size() > 0) {
				for (int i = 0; i < tempDeptList.size(); i++) {
					Dept tempDept = tempDeptList.get(i);
					String code = tempDept.getCode();
					List<Dept> tempDeptList1 = deptMapper.selectByParentCode(code);
					if (tempDeptList1 == null || tempDeptList1.size() == 0) {
						tempDeptList0.add(tempDept);
					} else {
						getAllDept(deptList, code);
					}
				}
				deptList.addAll(tempDeptList0);
			} else {
				Dept tempDept = deptMapper.selectByCode(deptCode);
				if (tempDept != null) {
					deptList.add(tempDept);
				}
			}
		}
		return deptList;
	}

	/**
	 * 获取部门id集合
	 * 
	 * @param params
	 * @return
	 * @throws Exception
	 */
	private List<String> getDeptIdList(Map<String, Object> params) throws Exception {
		List<String> deptIdList = new ArrayList<>();
		if (params.containsKey("deptCode")) {
			String deptCode = params.get("deptCode") == null ? "" : params.get("deptCode").toString();
			List<Dept> deptList = new ArrayList<>();
			getAllDept(deptList, deptCode);
			for (int i = 0; i < deptList.size(); i++) {
				deptIdList.add(deptList.get(i).getId());
			}
		}
		return deptIdList;
	}

	/**
	 * 获取部门名称
	 * 
	 * @param rankList
	 * @return
	 * @throws Exception
	 */
	private List<CountAnchorAndSalesRankListVO> getCountAnchorAndSalesRankListVOList(String deptCode, List<CountAnchorAndSalesRankListVO> rankList) throws Exception {
		if (rankList != null && rankList.size() > 0) {
			Map map = new HashMap();
			for (int i = 0; i < rankList.size(); i++) {
				rankList.get(i).setDeptName(getDeptName(deptCode, map, rankList.get(i)));
			}
		}
		return rankList;
	}

	/**
	 * 主播业绩排行榜
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public BasePage<CountAnchorAndSalesRankListVO> selectAnchorPerformanceRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		List<CountAnchorAndSalesRankListVO> rankList = new ArrayList<>();
		params.put("page", page);
		List<String> deptIdList = getDeptIdList(params);
		params.put("deptIdList", deptIdList);
		String deptCode = "";
		if (params.containsKey("deptCode")) {
			deptCode = params.get("deptCode") == null ? "" : params.get("deptCode").toString();
		}
		if (StringUtils.isNotEmpty(deptCode) && (deptIdList == null || deptIdList.size() == 0)) {
			return page;
		} else {
			rankList = this.countAnchorAndSalesRankListMapper.selectAnchorPerformanceRankListByPage(params);
		}
		getCountAnchorAndSalesRankListVOList(deptCode, rankList);
		page.setResults(rankList);
		return page;
	}

	/**
	 * 个人/工会排麦直播时长榜排行列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CountAnchorAndSalesRankListVO> selectPersonageLiveRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		params.put("page", page);
		List<CountAnchorAndSalesRankListVO> rankList = this.countAnchorAndSalesRankListMapper.selectPersonageLiveRankListByPage(params);
		if (rankList != null && rankList.size() > 0) {
			for (int i = 0; i < rankList.size(); i++) {
				CountAnchorAndSalesRankListVO rankListVO = rankList.get(i);
				// System.out.println("==selectPersonageLiveRankListByPage=="+rankListVO);
				int liveSecond = rankListVO.getLiveSecond();
				int hours = liveSecond / (60 * 60);
				int minutes = liveSecond % (60 * 60) / 60;
				int seconds = liveSecond % (60 * 60) % 60;
				if (hours > 0) {
					if (minutes > 0) {
						if (seconds > 0) {
							rankListVO.setLiveTime(hours + "小时" + minutes + "分" + seconds + "秒");
						} else {
							rankListVO.setLiveTime(hours + "小时" + minutes + "分");
						}
					} else {
						if (seconds > 0) {
							rankListVO.setLiveTime(hours + "小时" + seconds + "秒");
						} else {
							rankListVO.setLiveTime(hours + "小时");
						}
					}
				} else {
					if (minutes > 0) {
						if (seconds > 0) {
							rankListVO.setLiveTime(minutes + "分" + seconds + "秒");
						} else {
							rankListVO.setLiveTime(minutes + "分");
						}
					} else {
						rankListVO.setLiveTime(seconds + "秒");
					}
				}
				rankList.set(i, rankListVO);
			}
		}
		this.topDeptName(rankList);
		page.setResults(rankList);
		return page;
	}

	/**
	 * 内容平台个人/工会排麦直播时长榜排行列表导出
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CountAnchorAndSalesRankListVO> selectPersonageLiveRankList(Map<String, Object> params) throws Exception {
		List<CountAnchorAndSalesRankListVO> rankList = this.countAnchorAndSalesRankListMapper.selectPersonageLiveRankListByPage(params);
		this.topDeptName(rankList);
		return rankList;
	}

	/**
	 * 内容平台业绩榜排行列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CountAnchorAndSalesRankListVO> selectPerformanceRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		params.put("page", page);
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectPerformanceRankListByPage(params);
		this.topDeptName(list);
		page.setResults(list);
		return page;
	}

	/**
	 * 内容平台业绩榜排行列表导出
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CountAnchorAndSalesRankListVO> selectPerformanceRankList(Map<String, Object> params) throws Exception {
		List<CountAnchorAndSalesRankListVO> list = this.countAnchorAndSalesRankListMapper.selectPerformanceRankListByPage(params);
		this.topDeptName(list);
		return list;
	}

	/**
	 * 体系名称
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	private void topDeptName(List<CountAnchorAndSalesRankListVO> list) throws Exception {
		if (list != null && list.size() > 0) {
			for (CountAnchorAndSalesRankListVO countAnchorAndSalesRankListVO : list) {
				String deptId = countAnchorAndSalesRankListVO.getDeptId();
				if (deptId != null && StringUtils.isNotEmpty(deptId)) {
					Dept dept = this.deptMapper.selectByPrimaryKey(deptId);
					if (dept != null) {
						Dept resultDept = this.selectTopDept("", dept.getCode());
						if (resultDept != null) {
							countAnchorAndSalesRankListVO.setDeptName(resultDept.getName());
						}
					}
				}
			}
		}

	}

	/**
	 * 递归查找部门的顶级
	 * 
	 * @return
	 */
	private Dept selectTopDept(String tempDeptCode, String code) throws Exception {
		if (code == null || StringUtils.isBlank(code)) {
			return null;
		}
		Dept dept = this.deptMapper.selectByCode(code);
		if (dept == null) {
			return null;
		}
		if (dept.getType() != null) {
			if (tempDeptCode == null || StringUtils.isBlank(tempDeptCode)) {
				if (dept.getType() == 1) {
					return dept;
				} else {
					return selectTopDept(tempDeptCode, dept.getParentCode());
				}
			} else {
				if (dept.getParentCode().equals(tempDeptCode)) {
					return dept;
				} else {
					return selectTopDept(tempDeptCode, dept.getParentCode());
				}
			}
		} else {
			return selectTopDept(tempDeptCode, dept.getParentCode());
		}
	}

	/**
	 * 优质用户和有效注册
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public BasePage<CountAnchorAndSalesRankListVO> selectHighPlayerAndValidRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		params.put("page", page);
		int queryType = 1;
		if (params.containsKey("queryType")) {
			queryType = Integer.parseInt(params.get("queryType").toString());
		}
		List<CountAnchorAndSalesRankListVO> rankList = new ArrayList<>();
		List<String> deptIdList = getDeptIdList(params);
		params.put("deptIdList", deptIdList);
		String deptCode = "";
		if (params.containsKey("deptCode")) {
			deptCode = params.get("deptCode") == null ? "" : params.get("deptCode").toString();
		}
		if (StringUtils.isNotEmpty(deptCode) && (deptIdList == null || deptIdList.size() == 0)) {
			return page;
		} else {
			if (queryType == 1) // 优质用户
			{
				rankList = this.countAnchorAndSalesRankListMapper.selectNewHighPlayerRankListByPage(params);
			} else// 有效注册
			{
				rankList = this.countAnchorAndSalesRankListMapper.selectNewValidPlayerRankListByPage(params);
			}
		}
		getCountAnchorAndSalesRankListVOList(deptCode, rankList);
		page.setResults(rankList);
		return page;
	}

	/**
	 * 登录榜排行列表(包括3天和7天)
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CountAnchorAndSalesRankListVO> selectLoginPlayerRankListByPage(Map<String, Object> params, BasePage<CountAnchorAndSalesRankListVO> page) throws Exception {
		params.put("page", page);
		List<String> deptIdList = getDeptIdList(params);
		params.put("deptIdList", deptIdList);
		// System.out.println("==selectLoginPlayerRankListByPage=="+deptIdList+"===params=="+params);
		String deptCode = "";
		if (params.containsKey("deptCode")) {
			deptCode = params.get("deptCode") == null ? "" : params.get("deptCode").toString();
		}
		if (StringUtils.isNotEmpty(deptCode) && (deptIdList == null || deptIdList.size() == 0)) {
			return page;
		}
		List<CountAnchorAndSalesRankListVO> rankList = this.countAnchorAndSalesRankListMapper.selectLoginPlayerRankListByPage(params);
		getCountAnchorAndSalesRankListVOList(deptCode, rankList);
		page.setResults(rankList);
		return page;
	}
}
